package com.self.common.config;


import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @ClassName: Swagger2Config
 * @Description:
 * @Author: Mac
 * @Date: 2022/5/29 15:16
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class Swagger2Config {

    private SwaggerProperties swaggerProperties;

    public Swagger2Config(SwaggerProperties swaggerProperties) {
        this.swaggerProperties = swaggerProperties;
    }

    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
        //安全配置
        docket.securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
        return docket;

    }

    private List<SecurityContext> securityContexts() {
        return Arrays.asList(new SecurityContext(
                Arrays.asList(new SecurityReference(HttpHeaders.AUTHORIZATION,new AuthorizationScope[]{
                        new AuthorizationScope("global","access Resource")})),
                PathSelectors.any()));
    }

    private List<? extends SecurityScheme> securitySchemes() {
        return Arrays.asList(new ApiKey(HttpHeaders.AUTHORIZATION,HttpHeaders.AUTHORIZATION,HttpHeaders.AUTHORIZATION));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().contact(new Contact(swaggerProperties.getName(),
                swaggerProperties.getUrl(),swaggerProperties.getEmail()))
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
                .build();
    }
}
